package Client;



import Server.Bean.Express;
import Server.User;
import Server.View.View;

import java.io.*;
import java.net.Socket;

public class Client2 {
    //    初始化视图对象
    private static View v = new View();

    public Client2() throws IOException {
    }

    public static void main(String[] args)  {
        try {
            Socket socket = new Socket("127.0.0.1",8080);
            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            ObjectInputStream ois = new ObjectInputStream(is);
            //1.    欢迎
            v.welcome();
            m:while(true){
                //2.    身份选择菜单
                int m = v.menu();
                switch (m){//退出
                    case 0:
                        break m;
                    case 1://管理员
                        User login = v.login();
                        oos.writeChar('1');
                        oos.writeObject(login);
                        oos.flush();
                        boolean b1 = (boolean) ois.readObject();
                        if (b1) {//登陆成功
                            v.loginSuccessfully();
                            cClient(oos,ois);
                        }else{//登录失败
                            v.loginFailure();
                            break ;
                        }
                        break m;
                    case 2://用户
                        //取件成功一次就退出程序 取件不成功则一直获取取件码
                        //1.    获取用户输入的账号密码
                        User login2 = v.login();
                        //2.    将'1'和账号密码传给服务器
                        oos.writeChar('2');//身份码
                        oos.writeObject(login2);//输入的账号密码
                        oos.flush();
                        //3.    接受服务器传来的信息 登录成功返回true
                        boolean b2 = (boolean) ois.readObject();
                        if(b2){//登陆成功
                            v.loginSuccessfully();
                            //用户菜单
                            uClient(oos,ois);
                            v.bey();
                            break m;
                        }else{//登录失败
                            v.loginFailure();
                        }
                        break ;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务器维护中");
            return;
        }
        v.bey();
    }

    private static void uClient(ObjectOutputStream oos,ObjectInputStream ois) throws IOException, ClassNotFoundException {
        //1.    获取用户输入的取件码
        int code = v.uMenu();
        //2.    将取件码传到服务器
        oos.writeObject(code);
        oos.flush();
        //3.    接收查找的结果
        //      存在返回此快递对象 不存在返回null
        Express e = (Express) ois.readObject();
        if(e==null){//快递不存在
            v.expressNull();
            uClient(oos,ois);
        }else{//取件成功
            v.success();
            v.printExpress(e);
        }

    }

    private static int cClient(ObjectOutputStream oos,ObjectInputStream ois) throws Exception {
        while (true) {
            int mune = v.cMenu();//接收管理员
            oos.writeObject(mune);
            oos.flush();
            switch (mune){
                case 0:
                    return 0;
                case 1:{//快递录入
                    //1.    提示输入快递信息
                    Express e = v.insert();
                    oos.writeObject(e);
                    oos.flush();
                    if((Boolean)ois.readObject()){
                        e = (Express) ois.readObject();
                        v.printExpress(e);
                    }else {
                        v.expressExist();
                    }
                    break;
                }
                case 2:{//快递修改
                    //1.    提示输入快递信息
                    String number = v.findBynumber();
                    oos.writeObject(number);
                    if(!(Boolean)ois.readObject()){
                        v.expressNull();
                    }else {
                        Express e = (Express) ois.readObject();
                        v.success();
                        v.printExpress(e);
                        Express newe = v.update();
                        oos.writeObject(newe);
                        oos.flush();
                        newe = (Express) ois.readObject();
                        if(newe!=null){
                            v.printExpress(newe);
                        }else{
                            v.expressExist();
                        }
                    }
                }
                break;
                case 3:{//删除
                    //1.    输入快递单号
                    String number = v.findBynumber();
                    //2.    将单号传给服务器
                    oos.writeObject(number);
                    if(!(Boolean)ois.readObject()){
                        v.expressNull();
                    }else {
                        Express e = (Express) ois.readObject();
                        v.success();
                        v.printExpress(e);
                        oos.writeObject(v.delete());
                        oos.flush();
                    }
                }
                break;
                case 4:{
                    Express[] data = (Express[])ois.readObject();
                    v.printAllList(data);

                }
                break;
            }
        }
    }
}
